package com.atguigu.shangrongbao.core.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
public class ShangRongBaoCorsFilter implements WebMvcConfigurer {
    /**
     * 跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true)
                //放行哪些原始域
                .allowedOrigins("*")
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
                .allowedHeaders("*");
    }
    /**
     * 这种方式好像有冲突
     */
    // @Bean
    // public CorsFilter corsFilter(){
    //     log.info("cors设置", "===========");
    //     UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    //
    //     CorsConfiguration configuration = new CorsConfiguration();
    //     configuration.addAllowedOrigin("*");
    //     configuration.addAllowedMethod("*");
    //     configuration.addAllowedHeader("*");
    //     //是否允许携带cookie
    //     configuration.setAllowCredentials(true);
    //     log.info("configuration设置", configuration);
    //     source.registerCorsConfiguration("/**", configuration);
    //
    //     return new CorsFilter((CorsConfigurationSource) source);
    //
    // }
}
